package Demo4.test;

import Demo4.bean.Course;
import Demo4.bean.Student;
import Demo4.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test4 {
    @Test
    public void selectAll() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = build.openSession(true);

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        List<Student> students = mapper.selectAll();

        for (Student student : students) {
            System.out.println(student.getId()+student.getName()+student.getAge());
            List<Course> courses = student.getCourses();
            System.out.println("\t"+courses);
        }

        sqlSession.close();
        is.close();
    }
}
